var app = getApp();
var constract = require('../../utils/constract.js');
var util = require('../../utils/util.js');
var comm = require('../../utils/common.js');

/**
 * 
 * 用户id(uid)
 * 名片id(cardId)
 * type(名名用户,这里只有名名用户分享,默认为1)
 * 
 */
var uid, id, cardId,type=1, promptShow=false, showAuth;

function showOther(token,type,callback){
  wx.request({
    url: constract.other,
    header: { token: token },
    data: { passiveUid: uid, cardId: cardId, type: type },
    success: callback,
    fail: function(err) {
      console.log(err);
    }
  })
} 

Page({
  data: {
    erm:false,
    phoneX: false,
    getAuth: false
  },
  onLoad: function (options) {
    /*获取手机信息*/
    var that = this;

    wx.getSystemInfo({
      success: (info)=>{
        // console.log(info);
        // console.log(info.model);
        var isPhoneX = /iphone\s*x/i.test(info.model);
        that.setData({
          phoneX: isPhoneX,
        })
      }
    })

    console.log(options);
    cardId = options.id;
    uid = options.uid; //别人的用户id

    that.setData({
      //uid: uid,
      sourceList: app.globalData.sourceList, 
      cardId: cardId,
    })

    wx.request({
      url: constract.popilarity, // 人气增加接口
      data: { cardId: cardId}
    });

    app.Verification(function (token) {

      wx.request({
        url: constract.index, //判断存在当前用户信息
        header: { token: token },
        success: function (res) {
          // console.log(res);

          if(res.data.code === '23000') {
            that.setData({
              getAuth: true
            })
            return;
          }

          var userInfo = res.data.data.user[0]; //默认查看自己第一张名片
          if (res.data.data.state == 0) {
            promptShow=true;
            that.setData({
              createOrView: 0, //判断查看名片还是创建名片
              regState: res.data.data.state,
            })
          } else {
            that.setData({
              createOrView: 1
            })
          }
        }
      });

      showOther(token,type,function (res) {
        // console.log(res);
        if(+res.data.code !== 12000){
          that.setData({
            hasDel: true
          })
        } else {
          uid = res.data.data.uid; //重新获取uid,为老版本兼容
          id = res.data.data.id; //重新获取uid,为老版本兼容
          cardId = res.data.data.id;

          if (!res.data.data.state) {
            wx.showModal({
              title: '提示',
              content: '对方还没有创建名片！',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
            return;
          }
          wx.setNavigationBarTitle({
            title: res.data.data.name+"的名片",
          })
          that.setData({
            userInfo: res.data.data,
            uid: uid
          })
        }

      })
    })
  },
  onShow: function(){
    var that = this;
    app.Verification(function (token) {
      showAuth = app.globalData.getAuth;
      that.setData({
        showAuth: showAuth,
        getAuth: showAuth
      })

    })
    // this.setData({
    //   getAuth: app.globalData.getAuth
    // })
  },
  onHide: function(){
    var that = this;
    app.Verification(function (token) {
      showAuth = app.globalData.getAuth;
      that.setData({
        showAuth: showAuth,
        getAuth: showAuth
      })
    })
  },
  goAdd: function(){
    
    this.setData({
      promptShow: promptShow, 
      getAuth: showAuth, //判断是否授权
    })
  },
  goIndex: function(){
    wx.switchTab({
      url: '../index/index'
    })
  },
  praised: function () { //***********************************点赞接口
    var that = this;
    // var id = this.data.userInfo.id;
    var likeState = this.data.userInfo.likeState;
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) { 
          app.Verification(function (token) {
            wx.request({
              url: constract.likeAddOrEdit,
              header: { token: token },
              data: { like_uid: uid, type: 1,cardId: cardId },
              success: function (res) {
                // console.log(res);

                showOther(token,type,function (res) {
                  if (likeState != 0) {
                    that.setData({
                      userInfo: res.data.data,
                      flag: true
                    })
                  } else {
                    that.setData({
                      userInfo: res.data.data
                    })
                  }
                })
                
              }
            })
          })    
        } else {
          
          that.setData({
            showAuth: util.refusedAuth(),
            getAuth: util.refusedAuth()
          })
        }
      }
    })
  },

  collect: function (source) { //*************************************底栏收藏接口
    var that = this;
    // var id = this.data.userInfo.id;
    var collectState = this.data.userInfo.collectState;

    wx.getSetting({
      success: (res) => {
        // console.log(res);

        if (res.authSetting['scope.userInfo']) {
          app.Verification(function (token) {
            wx.request({
              url: constract.collectionAddOrEdit,
              header: { token: token },
              data: { collection_uid: uid, cardId: cardId, uid: id, /*source: source*/ }, // is_delete: collectState, 
              success: function (res) {
                // console.log(res);
                // console.log('-------------------------');

                showOther(token,type,function (res) {
                  // console.log(res);
                  that.setData({
                    userInfo: res.data.data
                  })
                })

              }
            })
          })
        } else {
          that.setData({
            showAuth: util.refusedAuth(),
            getAuth: util.refusedAuth()
          })
        }
      }
    })
  },


  collected: function (source) { //*************************************收藏人数接口
    var that = this;
    // var id = this.data.userInfo.id;
    var collectState = this.data.userInfo.collectState;

    wx.getSetting({
      success: (res) => {
        // console.log(res);
        if (res.authSetting['scope.userInfo']) {
          app.Verification(function (token) {
            wx.request({
              url: constract.collectionAddOrEdit,
              header: { token: token },
              data: { collection_uid: uid, cardId: cardId, uid: id, /*source: source*/},
              success: function (res) {
                showOther(token,type,function (res) {
                  if (collectState != 0) {
                    that.setData({
                      userInfo: res.data.data,
                      sep: true
                    })
                  } else {
                    that.setData({
                      userInfo: res.data.data
                    })
                  }
                })
              }
            })
          })
        } else {
          that.setData({
            showAuth: util.refusedAuth(),
            getAuth: util.refusedAuth()
          })
        }
      }
    })
  },

  promptCancel: function(){
    this.setData({
      promptShow: false
    })
  },
  onGotUserInfo: function(e){
    var that = this;
    comm.gotUserInfo(app,e,that);
  },

  hideAuth: function(){
    this.setData({
      getAuth: false,
      showAuth: false
    })
    
  },

  hasGetAuth: function(){
    app.globalData.getAuth = false;
    this.setData({
      showAuth: false,
      getAuth: false
    })
  },

  // choseSource:function(){ //调出弹框
  //   this.setData({
  //     showSource: true
  //   })
  // },
  // hideSources: function(){ //未选隐藏
  //   this.setData({
  //     showSource: false
  //   })
  // },
  // hasSource: function(e){ //选择隐藏
  //   var that = this;
  //   var source = e.currentTarget.dataset.source;

  //   if(source) {
  //     that.collected(source);
  //   }
  //   this.setData({
  //     showSource: false
  //   })
  // },

})